package com.nwork.qingmanhua;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.nwork.qingmanhua.adapter.CommonFragmentAdapter;
import com.nwork.qingmanhua.fragment.CampaignFragment;
import com.nwork.qingmanhua.fragment.CategoryFragment;
import com.nwork.qingmanhua.fragment.HumanFragment;
import com.nwork.qingmanhua.fragment.LatestFragment;
import com.nwork.qingmanhua.fragment.LeftMenuFragment;
import com.nwork.qingmanhua.fragment.MainFragment;
import com.nwork.qingmanhua.fragment.RightMenuFragment;

import java.util.LinkedList;
import java.util.List;


public class MainActivity extends SlidingFragmentActivity implements ViewPager.OnPageChangeListener {

    private ViewPager mainPager;
    private RadioGroup radioGroup;
    private RadioButton tuijianRaido;
    private RadioButton categoryRadio;
    private RadioButton latestRadio;
    private RadioButton humanRadio;
    private RadioButton camRadio;
    private ImageView barImg;
    private int screenWidth;//屏幕的 宽度
    private int currentIndex;//当前选中项
    private int topLayoutHeight;
    private float mCurrentCheckedRadioLeft;//当前被选中的RadioButton距离左侧的距离
    private HorizontalScrollView mHorizontalScrollView;
    private List<Fragment> fragments;
    private SlidingMenu mSlidingMenu;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initSlidingMenu();
        fragments = new LinkedList<>();
        //将Fragment加入ViewPager中
        MainFragment mainFragment = new MainFragment();
        fragments.add(mainFragment);
        CategoryFragment categoryFragment = new CategoryFragment();
        fragments.add(categoryFragment);
        LatestFragment latestFragment = new LatestFragment();
        fragments.add(latestFragment);
        HumanFragment humanFragment = new HumanFragment();
        fragments.add(humanFragment);
        CampaignFragment campaignFragment = new CampaignFragment();
        fragments.add(campaignFragment);
        iniController();
        LinearLayout topLayout = (LinearLayout) findViewById(R.id.top_layout);
        topLayout.measure(0, 0);
        topLayoutHeight = topLayout.getMeasuredHeight();
    }

    private void iniController() {
        radioGroup = (RadioGroup) findViewById(R.id.radio_group);
        tuijianRaido = (RadioButton) findViewById(R.id.radio_tui_jian);
        categoryRadio = (RadioButton) findViewById(R.id.radio_category);
        latestRadio = (RadioButton) findViewById(R.id.radio_latest);
        humanRadio = (RadioButton) findViewById(R.id.radio_human);
        camRadio = (RadioButton) findViewById(R.id.radio_cam);
        mHorizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizontal);
        mainPager = (ViewPager) findViewById(R.id.main_viewpager);
        barImg = (ImageView) findViewById(R.id.bar_image);
        CommonFragmentAdapter adapter =
                new CommonFragmentAdapter(getSupportFragmentManager(), fragments);
        mainPager.setAdapter(adapter);
        mainPager.setOnPageChangeListener(this);
        mainPager.setCurrentItem(0);
        initTabLineWidth();
    }


    /**
     * @param position             当前页面 点击滑动的压面
     * @param offset               当前页面滑动的百分比
     * @param positionOffsetPixels 当前页面偏移的像素
     */
    @Override
    public void onPageScrolled(int position, float offset, int positionOffsetPixels) {
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) barImg
                .getLayoutParams();
        if (currentIndex == 0 && position == 0)// 0->1
        {
            lp.leftMargin = (int) (offset * (screenWidth * 1.0 / 5) + currentIndex
                    * (screenWidth / 5));

        } else if (currentIndex == 1 && position == 0) // 1->0
        {
            lp.leftMargin = (int) (-(1 - offset)
                    * (screenWidth * 1.0 / 5) + currentIndex
                    * (screenWidth / 5));

        } else if (currentIndex == 1 && position == 1) // 1->2
        {
            lp.leftMargin = (int) (offset * (screenWidth * 1.0 / 5) + currentIndex
                    * (screenWidth / 5));
        } else if (currentIndex == 2 && position == 1) // 2->1
        {
            lp.leftMargin = (int) (-(1 - offset)
                    * (screenWidth * 1.0 / 5) + currentIndex
                    * (screenWidth / 5));
        } else if (currentIndex == 2 && position == 2) {//2->3
            lp.leftMargin = (int) (offset * (screenWidth * 1.0 / 5) + currentIndex
                    * (screenWidth / 5));
        } else if (currentIndex == 3 && position == 2) {//3-->2
            lp.leftMargin = (int) (-(1 - offset)
                    * (screenWidth * 1.0 / 5) + currentIndex
                    * (screenWidth / 5));
        } else if (currentIndex == 3 && position == 3) {//3-->4
            lp.leftMargin = (int) (offset * (screenWidth * 1.0 / 5) + currentIndex
                    * (screenWidth / 5));
        } else if (currentIndex == 4 && position == 3) {//4-->3
            lp.leftMargin = (int) (-(1 - offset)
                    * (screenWidth * 1.0 / 5) + currentIndex
                    * (screenWidth / 5));
        }
        barImg.setLayoutParams(lp);
    }

    private void initTabLineWidth() {
        DisplayMetrics dpMetrics = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay()
                .getMetrics(dpMetrics);
        screenWidth = dpMetrics.widthPixels;
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) barImg
                .getLayoutParams();
        lp.width = screenWidth / 5;
        barImg.setLayoutParams(lp);
    }

    private  void  initSlidingMenu(){
        LeftMenuFragment mLeftFragment=new LeftMenuFragment();
        RightMenuFragment rightMenuFragment=new RightMenuFragment();
        mSlidingMenu=getSlidingMenu();
        mSlidingMenu.setMode(SlidingMenu.LEFT_RIGHT); // 设置侧边栏菜单为左右模式
        setBehindContentView(R.layout.menu_left); // 设置左菜单的默认VIEW
        //mSlidingMenu.setMenu(R.layout.menu_left);
        mSlidingMenu.setBehindWidthRes(R.dimen.left_menu_width); // 设置左边菜单的宽度,该值为左菜单展开的宽度
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.LeftMenu,mLeftFragment).commit(); // 将左菜单默认VIEW替换为左菜单Fragment
        mSlidingMenu.setShadowDrawable(R.drawable.shadow); // 设置左菜单的阴影
        mSlidingMenu.setShadowWidth(10); // 设置阴影宽度
        mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN); // 设置侧边栏菜单触摸模式为全屏模式
        mSlidingMenu.setSecondaryMenu(R.layout.menu_right); // 设置右菜单默认VIEW
        mSlidingMenu.setSecondaryShadowDrawable(R.drawable.shadowright); // 设置右菜单阴影
        mSlidingMenu.setSecondaryBehindWidthRes(R.dimen.right_menu_width); // 设置右菜单的宽度,该值为右菜单展开的宽度
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.RightMenu,rightMenuFragment).commit();





    }


    @Override
    public void onPageSelected(int position) {
        currentIndex = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public int getTopLayoutHeight() {
        return topLayoutHeight;
    }
}
